package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.ProcFirstClassifyMain;
import com.sxbbc.common.core.entity.jz.ProcFirstClassifyShop;
import com.sxbbc.common.core.entity.jz.ProcSecondClassifyMain;
import com.sxbbc.common.core.entity.jz.ProcThirdClassifyMain;
import com.sxbbc.common.core.enums.ClassifyTypeEnums;
import com.sxbbc.common.core.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品主分类处理
 * @author leehao
 * @date 2019-04-30 15:31
 */
@SuppressWarnings("all")
@Service
public class ProcClassifyMainServiceImpl implements ProcClassifyMainService {
    @Autowired
    private IProcFirstClassifyMainService firstClassifyMainService;
    @Autowired
    private IProcSecondClassifyMainService secondClassifyMainService;
    @Autowired
    private IProcThirdClassifyMainService thirdClassifyMainService;
    @Autowired
    private IProcFirstClassifyShopService iProcFirstClassifyShopService;

    /* --------------- 定义分类操作中会使用到的标记量 --------------- */
    /** 新增一级分类 */
    private static final Integer CLASSIFY_FIRST_TOP = 0;
    /** 新增一级分类下级 */
    private static final int CLASSIFY_FIRST = 1;
    /** 新增二级分类下级 */
    private static final int CLASSIFY_SECOND = 2;
    /** 三级分类 */
    private static final int CLASSIFY_THIRD = 3;
    /** 新增标记 */
    private static final Integer ADD_FLAG = 1;
    /** 编辑标记 */
    private static final Integer UPDATE_FLAG = 2;

    @Override
    public List<ProcFirstClassifyMain> queryClassifyList(QueryWrapper<ProcFirstClassifyMain> wrapper) {
        // 一级分类
        List<ProcFirstClassifyMain> list = firstClassifyMainService.list(wrapper);
        if (!list.isEmpty() && list.size() > 0) {
            for (ProcFirstClassifyMain first : list) {
                // 二级分类
                QueryWrapper<ProcSecondClassifyMain> wrapperSecond = new QueryWrapper<>();
                wrapperSecond.eq("first_id", first.getId()).eq("status", StaticUtils.STATUS_YES).
                        eq("type", ClassifyTypeEnums.PLAT_FORM.getId());
                wrapperSecond.orderByDesc("sort");
                List<ProcSecondClassifyMain> secondList = secondClassifyMainService.list(wrapperSecond);
                first.setSecondClassifyMains(secondList);
                for (ProcSecondClassifyMain second : secondList) {
                    // 三级分类
                    QueryWrapper<ProcThirdClassifyMain> wrapperThird = new QueryWrapper<>();
                    wrapperThird.eq("second_id", second.getId()).eq("status", StaticUtils.STATUS_YES).
                            eq("type", ClassifyTypeEnums.PLAT_FORM.getId());
                    wrapperThird.orderByDesc("sort");
                    List<ProcThirdClassifyMain> thirdList = thirdClassifyMainService.list(wrapperThird);
                    second.setThirdClassifyMains(thirdList);
                }
            }
        }
        return list;
    }

    @Override
    public RestResponse deleteClassCategory(Integer id, Integer level) {
        boolean saveFlag = false;
        QueryWrapper<ProcSecondClassifyMain> secondWrapper = new QueryWrapper<>();
        QueryWrapper<ProcThirdClassifyMain> thirdWrapper = new QueryWrapper<>();
        List<ProcSecondClassifyMain> secondList = new ArrayList<>();
        List<ProcThirdClassifyMain> thirdList = new ArrayList<>();
        switch (level) {
            case 1:
                // 一级分类
                ProcFirstClassifyMain first = firstClassifyMainService.getById(id);
                if (first == null) {
                    return GetRest.getFail("该分类不存在");
                }
                secondWrapper.eq("first_id", first.getId()).eq("type", ClassifyTypeEnums.PLAT_FORM.getId()).eq("status", StaticUtils.STATUS_YES);
                thirdWrapper.eq("first_id", first.getId()).eq("type", ClassifyTypeEnums.PLAT_FORM.getId()).eq("status", StaticUtils.STATUS_YES);
                secondList = secondClassifyMainService.list(secondWrapper);
                thirdList = thirdClassifyMainService.list(thirdWrapper);
                if (!secondList.isEmpty() || secondList.size() > 0) {
                    secondList.forEach(x -> x.setStatus(StaticUtils.STATUS_NO));
                    saveFlag = secondClassifyMainService.updateBatchById(secondList);
                }
                if (!thirdList.isEmpty() || thirdList.size() > 0) {
                    thirdList.forEach(y -> y.setStatus(StaticUtils.STATUS_NO));
                    saveFlag = thirdClassifyMainService.updateBatchById(thirdList);
                }
                first.setStatus(StaticUtils.STATUS_NO);
                saveFlag = firstClassifyMainService.updateById(first);
                break;
            case 2:
                // 二级分类
                ProcSecondClassifyMain second = secondClassifyMainService.getById(id);
                if (second == null) {
                    return GetRest.getFail("该分类不存在");
                }
                thirdWrapper.eq("second_id", second.getId()).eq("type", ClassifyTypeEnums.PLAT_FORM.getId()).eq("status", StaticUtils.STATUS_YES);
                thirdList = thirdClassifyMainService.list(thirdWrapper);
                if (!thirdList.isEmpty() || thirdList.size() > 0) {
                    thirdList.forEach(x -> x.setStatus(StaticUtils.STATUS_NO));
                    saveFlag = thirdClassifyMainService.updateBatchById(thirdList);
                }
                second.setStatus(StaticUtils.STATUS_NO);
                saveFlag = secondClassifyMainService.updateById(second);
                break;
            case 3:
                // 三级分类
                ProcThirdClassifyMain third = thirdClassifyMainService.getById(id);
                if (third == null) {
                    return GetRest.getFail("该分类不存在或已删除");
                }
                third.setStatus(StaticUtils.STATUS_NO);
                saveFlag = thirdClassifyMainService.updateById(third);
                break;
            default:
                break;
        }
        if (!saveFlag) {
            throw new ThrowJsonException("删除分类失败");
        }
        return GetRest.getSuccess("删除成功");
    }

    @Override
    public boolean queryClassifyByPid(Integer id, Integer level) {
        boolean hasData = false;
        QueryWrapper<ProcSecondClassifyMain> secondWrapper = new QueryWrapper<>();
        QueryWrapper<ProcThirdClassifyMain> thirdWrapper = new QueryWrapper<>();
        List<ProcSecondClassifyMain> secondList = new ArrayList<>();
        List<ProcThirdClassifyMain> thirdList = new ArrayList<>();
        if (level < CLASSIFY_THIRD) {
            switch (level) {
                case 1:
                    ProcFirstClassifyMain first = firstClassifyMainService.getById(id);
                    if (first != null) {
                        secondWrapper.eq("first_id", first.getId()).
                                eq("type", ClassifyTypeEnums.PLAT_FORM.getId()).eq("status", StaticUtils.STATUS_YES);
                        secondList = secondClassifyMainService.list(secondWrapper);
                        if (!secondList.isEmpty() || secondList.size() > 0) {
                            hasData = true;
                        }
                    }
                    break;
                case 2:
                    ProcSecondClassifyMain second = secondClassifyMainService.getById(id);
                    if (second != null) {
                        thirdWrapper.eq("second_id", second.getId()).
                                eq("type", ClassifyTypeEnums.PLAT_FORM.getId()).eq("status", StaticUtils.STATUS_YES);
                        thirdList = thirdClassifyMainService.list(thirdWrapper);
                        if (!thirdList.isEmpty() || thirdList.size() > 0) {
                            hasData = true;
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        return hasData;
    }

    @Override
    public List<?> queryClassifyListAjax(Integer pid, Integer type, Integer targetId, Integer flag) {
        List<?> list = null;
        switch (flag){
            case CLASSIFY_FIRST:
                QueryWrapper<ProcFirstClassifyMain> firstWrapper = new QueryWrapper<>();
                firstWrapper.eq("status",StaticUtils.STATUS_YES).eq("type",type);
                if (targetId != null) {
                    firstWrapper.eq("target_id",targetId);
                }
                firstWrapper.or().eq("type",ClassifyTypeEnums.PLAT_FORM.getId()).eq("status",StaticUtils.STATUS_YES);
                list = firstClassifyMainService.list(firstWrapper);
                break;
            case CLASSIFY_SECOND:
                if (pid != null) {
                    QueryWrapper<ProcSecondClassifyMain> secondWrapper = new QueryWrapper<>();
                    secondWrapper.eq("first_id",pid).eq("status",StaticUtils.STATUS_YES).eq("type",type);
                    if (targetId != null) {
                        secondWrapper.eq("target_id",targetId);
                    }
                    secondWrapper.or().eq("type",ClassifyTypeEnums.PLAT_FORM.getId()).eq("status",StaticUtils.STATUS_YES).eq("first_id",pid);
                    list = secondClassifyMainService.list(secondWrapper);
                }
                break;
            case CLASSIFY_THIRD:
                if (pid != null) {
                    QueryWrapper<ProcThirdClassifyMain> thirdWrapper = new QueryWrapper<>();
                    thirdWrapper.eq("status",StaticUtils.STATUS_YES).eq("type",type).eq("second_id",pid);
                    if (targetId != null) {
                        thirdWrapper.eq("target_id",targetId);
                    }
                    thirdWrapper.or().eq("type",ClassifyTypeEnums.PLAT_FORM.getId()).eq("status",StaticUtils.STATUS_YES).eq("second_id",pid);
                    list = thirdClassifyMainService.list(thirdWrapper);
                }
                break;
            default:
                break;
        }
        return list;
    }

    @Override
    public List<?> queryClassifyListByPlat(ProcThirdClassifyMain thirdClassifyMain) {
        CalcuUtils calUtil = CalcuUtils.getInstance();
        //查询所有一级分类
        QueryWrapper mainWrapper = new QueryWrapper();
        mainWrapper.eq("type",ClassifyTypeEnums.PLAT_FORM.getId());
        mainWrapper.eq("status",StaticUtils.STATUS_YES);
        List<ProcFirstClassifyMain> firstList = firstClassifyMainService.list(mainWrapper);
        if(calUtil.isEquals(thirdClassifyMain.getLevel(),StaticUtils.FIRST_CLASSIFY)){
            return firstList;
        }else if(calUtil.isEquals(thirdClassifyMain.getLevel(),StaticUtils.SECOND_CLASSIFY)){
            //一级不为空查询二级
            mainWrapper.eq("first_id",thirdClassifyMain.getFirstId());
            List<ProcSecondClassifyMain> secondList = secondClassifyMainService.list(mainWrapper);
            return secondList;
        }else if(calUtil.isEquals(thirdClassifyMain.getLevel(),StaticUtils.THIRD_CLASSIFY)){
            //一二级不为空查询三级
            mainWrapper.eq("first_id",thirdClassifyMain.getFirstId());
            mainWrapper.eq("second_id",thirdClassifyMain.getSecondId());
            List<ProcThirdClassifyMain> thirdList = thirdClassifyMainService.list(mainWrapper);
            return thirdList;
        }else{
            return new ArrayList<>();
        }
    }

    /**
     * 新增分类
     *
     * @return com.function.common.RestResponse
     * @author leehao
     */
    public RestResponse addClassify(ProcThirdClassifyMain main) {
        boolean save = false;
        CalcuUtils cal = CalcuUtils.getInstance();
        ProcFirstClassifyMain first = new ProcFirstClassifyMain();
        Integer duplicateCount = 0;
        QueryWrapper<ProcFirstClassifyMain> firstWrapper = new QueryWrapper<>();
        QueryWrapper<ProcSecondClassifyMain> secondWrapper = new QueryWrapper<>();
        QueryWrapper<ProcThirdClassifyMain> thirdWrapper = new QueryWrapper<>();
        // 如果是对一级分类进行处理
        if (cal.isEquals(CLASSIFY_FIRST, main.getLevel())) {
            if (cal.isEquals(ADD_FLAG, main.getEditType())) {
                // 新增一级分类的下级
                //验证重复
                secondWrapper.eq("name",main.getName()).eq("status",StaticUtils.STATUS_YES);
                duplicateCount = secondClassifyMainService.count(secondWrapper);
                if (duplicateCount > 0) {
                    return GetRest.getFail("该名称的分类已存在！");
                }
                //新增操作
                ProcSecondClassifyMain second = new ProcSecondClassifyMain();
                second.setCreatetime(new Date());
                second.setName(main.getName());
                second.setSort(main.getSort());
                second.setStatus(StaticUtils.STATUS_YES);
                second.setTargetId(main.getTargetId());
                second.setFirstId(main.getFirstId());
                second.setUrl(main.getUrl());
                save = secondClassifyMainService.save(second);
            } else {
                // 编辑一级分类自己
                //重复性验证
                firstWrapper.eq("name",main.getName()).
                        eq("status",StaticUtils.STATUS_YES).
                        ne("id",main.getId());
                duplicateCount = firstClassifyMainService.count(firstWrapper);
                if (duplicateCount > 0) {
                    return GetRest.getFail("该名称的分类已存在");
                }
                //编辑保存操作
                first.setId(main.getId());
                first.setName(main.getName());
                first.setSort(main.getSort());
                first.setStatus(StaticUtils.STATUS_YES);
                first.setTargetId(main.getTargetId());
                first.setUrl(main.getUrl());
                save = firstClassifyMainService.updateById(first);
            }
        } else if (cal.isEquals(CLASSIFY_SECOND, main.getLevel())) {
            // 如果是二级分类
            if (cal.isEquals(ADD_FLAG, main.getEditType())) {
                // 新增二级分类的下级
                //重复验证
                thirdWrapper.eq("name",main.getName()).
                        eq("status",StaticUtils.STATUS_YES);
                duplicateCount = thirdClassifyMainService.count(thirdWrapper);
                if (duplicateCount > 0) {
                    return GetRest.getFail("该名称的分类已存在");
                }
                //新增操作
                ProcThirdClassifyMain third = new ProcThirdClassifyMain();
                third.setName(main.getName());
                third.setCreatetime(new Date());
                third.setSort(main.getSort());
                third.setStatus(StaticUtils.STATUS_YES);
                third.setUrl(main.getUrl());
                third.setTargetId(main.getTargetId());
                third.setFirstId(main.getFirstId());
                third.setSecondId(main.getSecondId());
                save = thirdClassifyMainService.save(third);
            } else {
                // 编辑二级分类自己
                //重复验证
                secondWrapper.eq("name",main.getName()).
                        eq("status",StaticUtils.STATUS_YES).
                        ne("id",main.getId());
                duplicateCount = secondClassifyMainService.count(secondWrapper);
                if (duplicateCount > 0) {
                    return GetRest.getFail("该名称的分类已存在");
                }
                //编辑操作
                ProcSecondClassifyMain second = new ProcSecondClassifyMain();
                second.setId(main.getId());
                second.setName(main.getName());
                second.setSort(main.getSort());
                second.setStatus(StaticUtils.STATUS_YES);
                second.setUrl(main.getUrl());
                second.setTargetId(main.getTargetId());
                save = secondClassifyMainService.updateById(second);
            }
        } else if (cal.isEquals(CLASSIFY_THIRD, main.getLevel()) && cal.isEquals(UPDATE_FLAG, main.getEditType())) {
            // 三级分类 只有编辑
            //编辑时重复验证
            thirdWrapper.eq("name",main.getName()).
                    eq("status",StaticUtils.STATUS_YES).
                    ne("id",main.getId());
            duplicateCount = thirdClassifyMainService.count(thirdWrapper);
            if (duplicateCount > 0) {
                return GetRest.getFail("该名称的分类已存在");
            }
            main.setStatus(StaticUtils.STATUS_YES);
            save = thirdClassifyMainService.updateById(main);
        } else if (cal.isEquals(CLASSIFY_FIRST_TOP, main.getLevel())) {
            // 新增一级分类
            //重复验证
            firstWrapper.eq("name",main.getName()).eq("status",StaticUtils.STATUS_YES);
            duplicateCount = firstClassifyMainService.count(firstWrapper);
            if (duplicateCount > 0) {
                return GetRest.getFail("该名称的分类已存在");
            }
            //新增操作
            first.setName(main.getName());
            first.setCreatetime(new Date());
            first.setSort(main.getSort());
            first.setStatus(StaticUtils.STATUS_YES);
            first.setUrl(main.getUrl());
            first.setTargetId(main.getTargetId());
            save = firstClassifyMainService.save(first);
        }
        if (!save) {
            throw new ThrowJsonException("保存分类失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    @Override
    public List<ProcFirstClassifyShop> queryBusinessFirstClassifyList(Integer bid) {
        QueryWrapper<ProcFirstClassifyShop> firstWrapper = new QueryWrapper<>();
        firstWrapper.eq("status",StaticUtils.STATUS_YES).eq("type",ClassifyTypeEnums.BUSINESS.getId());
        if (bid != null) {
            firstWrapper.eq("target_id",bid);
        }
        firstWrapper.or().eq("type",ClassifyTypeEnums.PLAT_FORM.getId()).eq("status",StaticUtils.STATUS_YES);
        return  iProcFirstClassifyShopService.list(firstWrapper);
    }
}
